package com.jack.leetcode.eachProblem06;

import java.util.Arrays;

public class 缺失的第一个正数 {

    public static void main(String[] args) {
        int[] a = {0, 2, 2, 1, 1};
        System.out.println(firstMissingPositive(a));
        int[] b = {1, 2, 2, 1, 3, 1, 0, 4, 0};
        System.out.println(firstMissingPositive(b));

    }

    public static int firstMissingPositive(int[] nums) {
        Arrays.sort(nums);
        int m = nums.length;
        int first = -1;
        for (int i = 0; i < m; i++) {
            if (nums[i] == 1) {
                first = i;
                break;
            }
        }
        if (first == -1) {
            return 1;
        }
        for (int i = first; i < m - 1; i++) {
            if (nums[i + 1] - nums[i] != 0 && nums[i + 1] - nums[i] != 1) {
                return nums[i] + 1;
            }
        }
        return nums[m - 1] + 1;
    }
}
